<?php
/**
 * Created by PhpStorm.
 * User: liyiming
 * Date: 2020/7/20
 * Time: 16:58
 */
//set_time_limit(0);
//error_reporting(E_ALL);
header("Content-type:text/html;charset:utf-8");
include_once('Logging.php');

class SLWTokenClass{

    public $DEBUG = true;
    public $MOD = '';

    const USER365ID_TEST = '13816388429';
    const PWD365_TEST = 'yuanzu123';
    const SERVICEKEY365_TEST = 'a83cdbffb8164b85a828cfcf97390871';

    const USER365ID = '18986273835';
    const PWD365 = '123456';
    const SERVICEKEY365 = '2c0634068b9f4a5d8e7def06ca4706ed';
    public function __construct($DEBUG,$MOD)
    {
        $this->DEBUG = $DEBUG;
        $this->MOD = $MOD;
    }

    public function token(){
        $type = 'slw_token';
        $where = array("AND" => array("=" => array('type'=>$type)));
        $access_token = $this->MOD->getItemByOther($where,'access_token');
        $row = $access_token[0];
        $user365Id = ($this->DEBUG) ? self::USER365ID_TEST : self::USER365ID;
        $pwd365 = ($this->DEBUG) ? self::PWD365_TEST : self::PWD365;
        $serviceKey365 = ($this->DEBUG) ? self::SERVICEKEY365_TEST : self::SERVICEKEY365;
        $SLW = new SLWExpressClass($this->DEBUG,$user365Id);
        $token = '';
        $slw = [$user365Id,$pwd365,$serviceKey365];
        if (is_array($row) && count((array)$row) > 0){
            $expires_in = $row['expires_in'] - 60;
            $createdt = $row['createdt'];
            $token_time = strtotime(date('Y-m-d H:i:s', strtotime('+ ' . $expires_in . ' seconds', strtotime($createdt))));
            $now = strtotime(date('Y-m-d H:i:s'));
            if ($now > $token_time) {
                $userLogin = $SLW->userLogin($slw);
                if($userLogin['errorcode'] == 0){
                    $token = $userLogin['token'];
                    $field_update = array();
                    $field_update['type'] = $type;
                    $field_update['access_token'] = $userLogin['token'];
                    $field_update['expires_in'] = $userLogin['expires_in'];
                    $field_update['createdt'] = date('Y-m-d H:i:s');
                    $where_fields = array("AND" => array("=" => array('type' => $type)));
                    $this->MOD->updateItem($field_update, $where_fields, 'access_token');
                }
            }else{
                $token = $row['access_token'];
            }
        }else{

            $userLogin = $SLW->userLogin($slw);
            if($userLogin['errorcode'] == 0){
                $field_update = array();
                $field_update['type'] = $type;
                $field_update['access_token'] = $userLogin['token'];
                $field_update['expires_in'] = $userLogin['expires_in'];
                $field_update['createdt'] = date('Y-m-d H:i:s');
                $token = $userLogin['token'];
                $this->MOD->addItem($field_update, 'access_token');

            }
        }
        return $token;
    }
}